﻿/*
* ==============================================================================
*
* FileName: AppSettings.cs
* Created: 2020/3/25 15:48:38
* Author: Yuhui
* Description: 
*
* ==============================================================================
*/
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;

namespace Information.System.Common
{
    /// <summary>
    /// appsettings.json操作类
    /// </summary>
    public class AppSettings
    {
        public static IConfiguration Configuration { get; set; }
        static AppSettings()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载   
            // var env = HostBuilderContext.HostingEnvironment;//可以通过WebHostBuilderContext类的HostingEnvironment属性得到IHostingEnvironment接口对象

            Configuration = new ConfigurationBuilder()
          .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
          //.Add(new JsonConfigurationSource { Path = $"appsettings.Development.json", ReloadOnChange = true })
          .Build();


        }
        public static void SetConfiguration(string environment) {
            if (!string.IsNullOrEmpty("environment"))
            {
                Configuration = new ConfigurationBuilder()
                    .Add(new JsonConfigurationSource { Path = $"appsettings.{environment}.json", ReloadOnChange = true })
               .Build();
            }
            else
            {
                Configuration = new ConfigurationBuilder()
              .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
              //.Add(new JsonConfigurationSource { Path = $"appsettings.Development.json", ReloadOnChange = true })
              .Build();
            }
        }
        /// <summary>
        /// 获得配置文件的对象值
        /// </summary>
        /// <param name="jsonPath"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetJson(string jsonPath, string key)
        {
            IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build(); //json文件地址
            string s = config.GetSection(key).Value; //json某个对象
            return s;
        }
    }
}
